<?php
/*
[UCenter Home] (C) 2007-2008 Comsenz Inc.
$Id: sendmail.php 8302 2008-08-01 02:10:28Z liguode $
*/
if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

//防止超时
set_time_limit(0);

//一次发送邮件个数
$pernum = 5;

include_once(S_ROOT.'./source/function_sendmail.php');

//获取发送队列
$emails = $touids = array();
$query = $_SGLOBAL['db']->query("SELECT touid, email FROM ".tname('mailcron')." WHERE sendtime<='$_SGLOBAL[timestamp]' ORDER BY sendtime LIMIT 0,$pernum");
while ($value = $_SGLOBAL['db']->fetch_array($query)) {
	if($value['email']) {
		$emails[$value['email']] = $value['email'];
	} elseif ($value['touid']) {
		$touids[$value['touid']] = $value['touid'];
	}
}

//获取用户邮件
$useremails = $deluids = array();
if($touids) {
	$query = $_SGLOBAL['db']->query("SELECT sf.emailcheck, sf.email, s.uid, s.lastsend, s.updatetime
									FROM ".tname('space')." s
									LEFT JOIN ".tname('spacefield')." sf ON sf.uid=s.uid
									WHERE s.uid IN (".simplode($touids).")");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		if($_SCONFIG['sendmailday'] && $value['emailcheck'] && $value['email'] && ($_SGLOBAL['timestamp'] - $value['lastlogin'] > $_SCONFIG['sendmailday']*86400)) {
			$useremails[$value['uid']] = $value['email'];
		} else {
			$deluids[$value['uid']] = $value['uid'];
			unset($touids[$value['uid']]);
		}
	}
}



//删除无效的用户邮件
if($deluids) {
	$_SGLOBAL['db']->query("DELETE FROM ".tname('mailcron')." WHERE touid IN (".simplode($deluids).")");
	$_SGLOBAL['db']->query("DELETE FROM ".tname('mailqueue')." WHERE touid IN (".simplode($deluids).")");
}

//获取邮件队列
$wherearr = $sends = $sendmails = array();
if($emails) {
	$wherearr[] = 'email IN ('.simplode($emails).')';
}
if($touids) {
	$wherearr[] = 'touid IN ('.simplode($touids).')';
}

if($wherearr) {
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mailqueue')." WHERE ".implode(' OR ', $wherearr));
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		if($value['touid']) {
			$value['email'] = $useremails[$value['touid']];
		}
		$sendmails[$value['email']] = $value['email'];
		$sends[$value['email']]['subject'][] = $value['subject'];
		$sends[$value['email']]['message'][] = $value['message'];
		$sends[$value['email']]['dateline'][] = $value['dateline'];
	}
	//删除当前已执行的队列
	$_SGLOBAL['db']->query("DELETE FROM ".tname('mailcron')." WHERE ".implode(' OR ', $wherearr));
	$_SGLOBAL['db']->query("DELETE FROM ".tname('mailqueue')." WHERE ".implode(' OR ', $wherearr));

	//更新用户最后发送时间
	if($touids) {
		$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET lastsend='$_SGLOBAL[timestamp]' WHERE uid IN (".simplode($touids).")");
	}
}

//开始发送邮件
foreach ($sendmails as $email) {
	$thesend = $sends[$email];
	$subject = $thesend['subject'][0];
	$message = '';
	foreach ($thesend['subject'] as $key => $value) {
		$gmdateline = sgmdate('m-d H:i', $thesend['dateline'][$key]);
		if($thesend['message'][$key]) {
			$message .= "<strong>$value</strong> (".$gmdateline.")\n".$thesend['message'][$key]."\n";
		} else {
			$message .= "$value (".$gmdateline.")\n";
		}
	}
	sendmail(array($email), $subject, $message);
}

?>